<?php
/**
* 财务处理
*/
class FinanceAction extends CommonAction
{
	
	public function index()
	{
		$thisMonth = empty($_POST['month']) ? date('m') : $_POST['month'];
		$total_money = M('Sale')->sum('total');
		$this->display();
	}

    /**
    * 获取单个产品的采购情况，包括采购总价，采购明细，采购总数量，移动平均价等。可按时间段，供应商来分别统计
    *@ comon_product_id 产品编号
    *@ supplier_id 供应商编号
    *@ return_type 1,返回采购总价，2返回采购总数量；3 返回移动平均价；4返回采购明细；5返回全部 .默认返回总计数据
    *@ start_time 起始时间，默认为空，可取有历史记录以来的时间
    *@ end_time 截止时间，默认为空，取当前时间
    **/
    public static function getProductBuyInfo($common_product_id,$return_type=0,$supplier_id=0,$start_time=0,$end_time=0 ){
        $map = array();
        if($supplier_id != 0 )
            $map['supplier_id'] = $supplier_id;
        //time 
        $cond_time = array();
        if( $start_time != 0 )
          array_push($cond_time, array( 'egt' , strtotime($start_time ) ));
        if( $end_time != 0 )
          array_push($cond_time, array( 'elt' , strtotime($end_time) ) );
        //TODO time type

        if(count($cond_time) > 0 )
            $map['create_time'] = $cond_time;
        $map['common_id'] = $common_product_id;
        if($return_type >3 )
           $enter_details = D('Enter_detailView')->where($map)->findAll();
        if($return_type != 4 )
           $buy_total= D('Enter_detailView')->where($map)->field('sum(total) as total_money,sum(number) as total_number')->find();
        
        switch($return_type){
           case 1:
              return $buy_total['total_money'];
              break;
          case 2:
              return $buy_total['total_number'];
              break;
          case 3: //移动平均价
              return $buy_total['total_money']/$buy_total['total_number'];
              break;
          case 4:
              return $enter_details;
              break;
          case 5:
              $result = array(
                 'detail' => $enter_details,
                 'total_money' => $buy_total['total_money'],
                 'total_number' => $buy_total['total_number']
              );
              return $result;
              break;
         default:
              return $buy_total;
        }
            
    }

     /**
    * 获取单个产品的(零售)销售情况，包括销售总金额，零售明细，总数量，移动平均售价等。可按时间段，销售来源分别统计
    *@ comon_product_id 产品编号
    *@ order_source_id 供应商编号
    *@ return_type 1,返回零售总金额，2返回总数量；3 返回移动平均售价；4返回零售明细；5返回全部 .默认返回总计数据
    *@ time_type 0 默认发货时间，1 为下单时间
    *@ start_time 起始时间，默认为空，可取有历史记录以来的时间
    *@ end_time 截止时间，默认为空，取当前时间
    *@ TODO 折价未计算
    **/
    public static function getProductSaleInfo($common_product_id,$return_type=0,$order_source_id=0,$time_type=0,$start_time=0,$end_time=0 ){
        $map = array();
        $map['_string'] = "order_status =3 or order_status = 4"; //仅统计已发货及交易成功的订单
        if($order_source_id != 0 )
            $map['order_source_id'] = $order_source_id;
        //time 
        $cond_time = array();
        if( $start_time != 0 )
          array_push($cond_time, array( 'egt' , strtotime($start_time ) ));
        if( $end_time != 0 )
          array_push($cond_time, array( 'elt' , strtotime($end_time) ) );
        //TODO time type

        if(count($cond_time) > 0 ){
          switch ($time_type) {
              case '1': //create时间
                $map['Sale.create_time '] = $cond_time;
                break;
              case '0': //发货时间
                $map['Sale.shipping_time '] = $cond_time;
                break;
          }
        }
        $map['common_id'] = $common_product_id;
        if($return_type >3 )
           $enter_details = D('Sale_detailView')->where($map)->findAll();
        if($return_type != 4 )
           $buy_total= D('Sale_detailView')->where($map)->field('sum(Sale_detail.total) as total_money,sum(Sale_detail.number) as total_number')->find();
        
        switch($return_type){
           case 1:
              return $buy_total['total_money'];
              break;
          case 2:
              return $buy_total['total_number'];
              break;
          case 3: //移动平均价
              return $buy_total['total_money']/$buy_total['total_number'];
              break;
          case 4:
              return $enter_details;
              break;
          case 5:
              $result = array(
                 'detail' => $enter_details,
                 'total_money' => $buy_total['total_money'],
                 'total_number' => $buy_total['total_number']
              );
              return $result;
              break;
         default:
              return $buy_total;
        }
            
    }

}
?>
